﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
using System.Text;



public class LoadShader : MonoBehaviour {
    public bool LoadTest;
    public void LoadTestMethod()
    {
        StartCoroutine(LoadAb(string.Format("file://{0}/../Abs/bodyab", Application.dataPath)));
    }
    public Shader shader;
    public static AssetBundle bundle;
    public string resName = "assets/assetbundle/materials/chara_00/female/body/type_00/materials/cf_m_mnpb.mat";
    public Object[] allObjects;
    public Material mat;
    // Use this for initialization
    public static IEnumerator LoadAb (string fp) {
        yield return null;
        //var url = string.Format("file://{0}/../AB/mat_cha_00.unity3d", Application.dataPath);
        var url = fp;
        Debug.LogError("Url:"+url);
        //var req = UnityWebRequest.GetAssetBundle(url);
        var req = new WWW(url);
        yield return req;
        bundle = req.assetBundle;
        Debug.LogError(bundle);
        /*
        req.SendWebRequest();
        Debug.LogError("StartLoad:");
        while (!req.isDone)
        {
            yield return null;
        }
        Debug.LogError("LoadFinish");
        bundle = DownloadHandlerAssetBundle.GetContent(req);
        req.Dispose();
        shader = bundle.LoadAsset<Shader>("PBRsp");
        var names = bundle.GetAllAssetNames();
        var sb = new StringBuilder();
        foreach(var b in names)
        {
            sb.AppendLine(b);
        }
        File.WriteAllText(Path.Combine(Application.dataPath, "../names.txt"), sb.ToString());

        mat = bundle.LoadAsset<Material>(resName);
        allObjects = bundle.LoadAllAssets<Shader>();
        Debug.LogError("LoadAllAssets"+allObjects.Length);
        */
    }

    public bool Click = false;
    private void Update()
    {
        if (Click)
        {
            Click = false;
            mat = bundle.LoadAsset<Material>(resName);
        }
    }

}
